package com.heartware.sh.blackhole.retrofit;

import com.heartware.sh.blackhole.BuildConfig;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/11/30 0030.
 * 工厂模式, retrofit服务工厂
 */
public class ServiceFactory {

    public static <T> T createServiceFrom(final Class<T> cls, String base_api){

        OkHttpClient httpClient = new OkHttpClient();
        if (BuildConfig.DEBUG) { //网络请求日志开关, debug模式开启,
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            httpClient = new OkHttpClient.Builder().addInterceptor(logging).build();
        }

        Retrofit adapter = new Retrofit.Builder()
                .baseUrl(base_api)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient)
                .build();

        return adapter.create(cls);

    }

}
